// pages/test/test.js
var Login = require("../../Controls/Login")
var Protocol = require("../../Controls/Protocol")
var util = require ("../../utils/util")
import area from "../../pages/addressedit/area.js"
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js'
const moment = require("../../miniprogram_npm/moment/index.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    login_state:"授权登录",
    publics_state:"发布测试数据",
    check_state:"查看产品id=2",
    my_state:"我的",
    isShow:false,
    area:area,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var now = moment();
    console.log(now.format('YYYYMMDDHHmmss'));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }, 
  btnGetUserInfoIvk:function()
  {
    Login.login(res=>{
      this.setData({ login_state: res.userinfo.nickName + "已登陆"})
    });
  },
  btnPublishIvk:function()
  {
    var data =
      {
        name: "测试商品",
        maxPrice: 100000,
        minPrice: 0,
        qty: 10,
        desc: "这是测试商品描述",
        duration: 3600,
        images: "www.baidu.com",
      }
    //测试数据
    Protocol.publish(data);
  },
  btnCheckDetailIvk:function()
  {
    Protocol.productDetail(2);
  },
  btnMyIvk:function()
  {
    Protocol.myInfo();
  },
  btnMyPublishedIvk:function()
  {
    Protocol.myPublished(1);
  },
  btnMyAttandedIvk: function()
  {
    Protocol.myAttanded(1);
  },
  btnMyGotIvk: function () 
  {
    Protocol.myGot(1);
  },
  btnSubscribeIvk:function()
  {
    Protocol.subscribe(1)
  },
  btnTimeIvk:function()
  {
    var r = util.getLeftTime(10001);
    console.log(r);
  },
  btnImageCropperIvk:function()
  {
    wx.navigateTo({
      url: '../../vendor/we-cropper/dashboard/index',
    })
  },
  btnQRIvk:function()
  {

    var Session = require ("../../vendor/wafer2-client-sdk/lib/session")
    var session = Session.get();
    console.log(session);
    var token = session.id;

    wx.request({
      url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`,
      data:
      {
        scene:"1",
        page:"pages/balance/balance",
      },
      success:function(res)
      {
        console.log(res);
      },
      fail:function(res)
      {
        console.log(res);
      }
    })
  },
  btnClearTimesIvk:function()
  {
    Protocol.clearPublishTimes();
  },

  btnBargainIvk:function()
  {
    var id = 18;
    Protocol.bargain(id);
  },

  btnTokenIvk:function()
  {
    var scene = "123"
    Protocol.getToken(scene); 
  },
  btnareaIvk:function()
  {
    this.setData({isShow:true});
  },
  btnPlaceOrder: function()
  {

    var now = moment();
    var timeStamp = Math.ceil((now.valueOf() / 1000)).toString();
    console.log("下单时间戳："+timeStamp);

    const order = 
    {
      timeStamp:timeStamp,
      amount:500,
      body:'充值测试'
    }

    Protocol.placeOrder(order, {
      success:function(res)
      {
        var data = res.data.data;
        console.log(data);
        
        if(data.isOk == true)
        {
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonce_str,
            package: 'prepay_id='+data.prepay_id,
            signType: 'md5',
            paySign: data.paySign,
            success: (res)=>
            {
              console.log("调用支付成功");
              console.log(res);
              Protocol.queryOrder(data.tradeId);
            },
            fail: (res)=>
            {
              console.log("调用支付失败");
              console.log(res);
            },
          });
        }
        else
        {
          console.log("下单失败："+data.msg);
        }
        
      },
      fail:function(res)
      {

      }
    });
  },
  btnShowProducts:function()
  {
    const page = 1;
    const tag = "hotspot"
    Protocol.showProducts(page, tag);
  },
  btnGetProductConfig:function()
  {
    Protocol.getProductConfig();
  },

  btnAddTimes:function()
  {
    Protocol.commond('addtimes');
  },

  btnEncashmentIvk: function()
  {
    Protocol.encashment('5', {
      success: ()=>
      {

      }
    });
  }
})